#include <vector>
using namespace std;

int ans;
int temp[50003];

void mergeSort(vector<int>& nums, int l, int r) {
    if (l >= r) return;
    int mid = l + (r - l) / 2;
    mergeSort(nums, l, mid);
    mergeSort(nums, mid + 1, r);
    int i = l, j = mid + 1;
    int cur = l;
    int cnt = 0;
    while (i <= mid && j <= r) {
        if (nums[i] > nums[j]) {
            cnt++;
            temp[cur++] = nums[j++];
        } else {
            ans += cnt;
            temp[cur++] = nums[i++];
        }
    }
    if (i == mid + 1) {
        while (j <= r) temp[cur++] = nums[j++];
    } else {
        while (i <= mid) {
            temp[cur++] = nums[i++];
            ans += cnt;
        }
    }
    for (int t = l; t <= r; t++) nums[t] = temp[t];
}

int reversePairs(vector<int>& nums) {
    mergeSort(nums, 0, nums.size() - 1);
    return ans;
}